<?php


namespace Module\AdminApi\Api\Controller;


use Illuminate\Routing\Controller;
use ModStart\Core\Assets\AssetsUtil;
use ModStart\Core\Exception\BizException;
use ModStart\Core\Input\InputPackage;
use ModStart\Core\Input\Response;
use ModStart\Data\AbstractDataStorage;
use ModStart\Data\DataManager;

/**
 * @Api 基础对接API
 */
class UploadController extends Controller
{
    private function upload($category)
    {
        $input = InputPackage::buildFromInput();
        $filename = $input->get('filename');
        $content = $input->getBase64Image('base64Content');
        BizException::throwsIfEmpty('filename为空', $filename);
        BizException::throwsIfEmpty('content为空', $content);
        $ret = DataManager::upload($category, $filename, $content);
        BizException::throwsIfResponseError($ret);
        $data = $ret['data']['data'];
        $path = AbstractDataStorage::DATA . '/' . $data['category'] . '/' . $data['path'];
        return Response::generateSuccessData([
            'id' => $data['id'],
            'fileSize' => $data['size'],
            'md5' => $data['md5'],
            'path' => $path,
            'fullPath' => AssetsUtil::fixFull($ret['data']['fullPath'], false),
            //'raw' => $ret,
        ]);
    }

    private function uploadTemp($category)
    {
        $input = InputPackage::buildFromInput();
        $filename = $input->get('filename');
        $content = $input->getBase64Image('base64Content');
        BizException::throwsIfEmpty('filename为空', $filename);
        BizException::throwsIfEmpty('content为空', $content);
        $ret = DataManager::uploadToTemp($category, $filename, $content);
        BizException::throwsIfResponseError($ret);
        $dataTemp = $ret['data']['dataTemp'];
        $path = AbstractDataStorage::DATA . '/' . $dataTemp['category'] . '/' . $dataTemp['path'];
        return Response::generateSuccessData([
            'id' => $dataTemp['id'],
            'fileSize' => $dataTemp['size'],
            'path' => $path,
            'fullPath' => AssetsUtil::fixFull($ret['data']['fullPath'], false),
            // 'raw' => $ret,
        ]);
    }

    /**
     * @Api 上传图片
     * @ApiBodyParam ADMIN_API_KEY string 对接Key
     * @ApiBodyParam filename string 文件名（带后缀，如 image.jpg）
     * @ApiBodyParam base64Content string base64编码的文件内容
     * @ApiResponseData {
     * "id": 586,
     * "fileSize": 7,
     * "md5": "0970c14e28eca68f49b23a36a35debe6",
     * "path": "data/file/2022/10/15/9191_jqoi_2518.jpg",
     * "fullPath": "http://example.com/data/file/2022/10/15/9191_jqoi_2518.jpg"
     * }
     */
    public function image()
    {
        return $this->upload('image');
    }

    /**
     * @Api 上传图片(临时)
     * @ApiDesc 临时上传的文件会在1天内系统自动清除
     * @ApiBodyParam ADMIN_API_KEY string 对接Key
     * @ApiBodyParam filename string 文件名（带后缀，如 image.jpg）
     * @ApiBodyParam base64Content string base64编码的文件内容
     * @ApiResponseData {
     * "id": 142,
     * "fileSize": 7,
     * "path": "data/image/y3zhewznwibcsnzws5sgmyxjfqr62syg.jpg",
     * "fullPath": "http://example.com/data_temp/image/y3zhewznwibcsnzws5sgmyxjfqr62syg.jpg"
     * }
     */
    public function imageTemp()
    {
        return $this->uploadTemp('image');
    }

    /**
     * @Api 上传文件
     * @ApiBodyParam ADMIN_API_KEY string 对接Key
     * @ApiBodyParam filename string 文件名（带后缀，如 image.txt）
     * @ApiBodyParam base64Content string base64编码的文件内容
     * @ApiResponseData {
     * "id": 586,
     * "fileSize": 7,
     * "md5": "0970c14e28eca68f49b23a36a35debe6",
     * "path": "data/file/2022/10/15/9191_jqoi_2518.txt",
     * "fullPath": "http://example.com/data/file/2022/10/15/9191_jqoi_2518.txt"
     * }
     */
    public function file()
    {
        return $this->upload('file');
    }

    /**
     * @Api 上传文件(临时)
     * @ApiDesc 临时上传的文件会在1天内系统自动清除
     * @ApiBodyParam ADMIN_API_KEY string 对接Key
     * @ApiBodyParam filename string 文件名（带后缀，如 image.jpg）
     * @ApiBodyParam base64Content string base64编码的文件内容
     * @ApiResponseData {
     * "id": 142,
     * "fileSize": 7,
     * "path": "data/image/y3zhewznwibcsnzws5sgmyxjfqr62syg.jpg",
     * "fullPath": "http://example.com/data_temp/image/y3zhewznwibcsnzws5sgmyxjfqr62syg.jpg"
     * }
     */
    public function fileTemp()
    {
        return $this->uploadTemp('file');
    }
}
